package com.uestc.shop.web;

import com.uestc.shop.domain.User;
import com.uestc.shop.domain.dto.UserInfoDto;
import com.uestc.shop.domain.dto.UserLoginDto;
import com.uestc.shop.domain.dto.UserProfileDto;
import com.uestc.shop.domain.dto.UserRegDto;
import com.uestc.shop.service.IUserService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * @Package: com.uestc.shop.web
 * @author: Jounghu
 * @date: 2018/7/26 1:29
 * @version: V1.0
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

    @Autowired
    ModelMapper modelMapper;

    /**
     * 用户登录入口
     *
     * @return
     */
    @PostMapping("/login")
    public String login(UserLoginDto userLoginDto, HttpServletRequest request) {
        if (StringUtils.isEmpty(userLoginDto.getUsername()) || StringUtils.isEmpty(userLoginDto.getPassword())) {
            request.setAttribute("loginMsg", "登录名或者密码为空!");
            return "Login";
        }
        User user = userService.findUserByName(userLoginDto.getUsername());
        if (user == null) {
            request.setAttribute("loginMsg", "用户名不存在!");
            return "Login";
        }

        if (!StringUtils.pathEquals(user.getPassword(), userLoginDto.getPassword())) {
            request.setAttribute("loginMsg", "密码错误错误!");
            return "Login";
        }

        UserInfoDto userInfoDto = modelMapper.map(user, UserInfoDto.class);
        //登录成功，把用户名放入Session
        request.getSession().setAttribute("user", userInfoDto.getUsername());

        return "redirect:/index";
    }

    @PostMapping("/reg")
    public String doReg(UserRegDto userRegDto, HttpServletRequest request) {

        User user = modelMapper.map(userRegDto, User.class);

        user = userService.save(user);

        if (user == null) {
            //注册失败，提醒一下
            request.setAttribute("regMsg", "注册失败，请重新再试！");
            return "register";
        }

        request.getSession().setAttribute("user", userRegDto.getUsername());
        return "redirect:/index";
    }


    @PostMapping("/profile")
    public String doProfile(UserProfileDto userProfileDto, HttpServletRequest request) {
        int userId = userProfileDto.getId();
        if (userId == 0) {
            //非法用户回去登录
            return "redirect:/login";
        }

        User user = userService.findUserById(userId);
        if (user == null) {
            //非法用户
            return "redirect:/login";
        }

        user.setEmail(userProfileDto.getEmail());
        user.setPassword(userProfileDto.getPassword());
        user = userService.save(user);

        request.setAttribute("userprofile", userProfileDto);
        if (user == null) {
            //注册失败，提醒一下
            request.setAttribute("updateMsg", "修改失败，请重新再试！");
            return "profile";
        }
        request.setAttribute("successMsg", "修改成功！");
        request.getSession().setAttribute("user", user.getUsername());
        return "profile";
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest request) {
        Enumeration em = request.getSession().getAttributeNames();
        while(em.hasMoreElements()){
            request.getSession().removeAttribute(em.nextElement().toString());
        }
        return "redirect:/index";
    }

}
